<template>
	<view >
		<view class="flex align-c nav" :style="{paddingTop: statusBar + 'px'}">
			<image src="/static/home/back.png" mode="widthFix" class="nav_back_icon" @click="$back" />
			<view class="f1">
				<view class="nav_seach_box flex align-c">
					<image src="/static/home/seach-icon-gray.png" mode="aspectFill" class="nav_seach_icon" />
					<input type="text" class="input-box" v-model="keyword" @confirm="getCourseList" placeholder="搜索课程" placeholder-class="p-c">
				</view>
			</view>
			<view class="flex align-c nav_right" @click="open('show')">
				<image src="/static/home/sort-icon.png" mode="aspectFill" class="nav_sort-icon" />
				<view v-if="typeIndex|| typeIndex === 0">{{ typeList[typeIndex].name }}</view>
				<view v-else>请选择</view>
			</view>
		</view>
		
		<scroll-view v-if="type == 2" scroll-x="true" class="flex align-c tabs" :style="{top: statusBar+ 44 + 'px'}">
			<view class="tabs_item" v-for="(v,i) in navdatas" :key="i" @click="checkNav(v.id)">
				<view class="item-name" :class="{'act': class_id == v.id}" >{{ v.name }}</view>
				<view class="tabs_line" :class="{'tabs_line_act': class_id == v.id}"></view>
			</view>
		</scroll-view>
		
		<view :style="{height: `${type == 2? statusBar +88 : statusBar +44}` +'px'}" />
		
		<view class="course" v-for="(v,i) in list" :key="i">
			<view class="course_content flex" @click="$to(`/pages/home/overt/course_detail?id=${v.id}&type=${type}`)">
				<image v-if="v && v.cover" :src="$img(v.cover)" mode="aspectFill" class="course_content_img" />
				<image v-else src="/static/course.png" mode="aspectFill" class="course_content_img" />
				<view class="f1 between column">
					<view class="">
						<view class="fw-b" v-if="v && v.title">{{ v.title }}</view>
						<view class="course_content_label" v-if="v && v.start_at">时间：{{ v.start_at }}</view>
					</view>
					<view class="course_content_price" v-if="v.type != 1">
						<text class="symbol">￥</text>
						<text class="fw-b">{{ v.price }}</text>
						<text class="util">/节</text>
					</view>
				</view>
			</view>
			<view class="course_bottom flex align-c">
				<view class="flex align-c" @click="delItem(v.id)">
					<image src="/static/home/del-icon-gray.png" mode="widthFix" class="del-icon-gray" />
					<view class="gray">删除</view>
				</view>
				<view class="f1 flex align-c course_bottom_box">
					<button class="btn org_btn" v-if="v.status == 3 ||v.status == 5" @click="onShelf(v.id)">上架</button>
					<button class="btn" v-if="v.status == 4" @click="offShelf(v.id)">下架</button>
					<button class="btn" v-if="v.status != 1 && v.status != 4" @click="$to(`/pages/home/publish/publish?id=${v.id}&type=${type}`)">编辑</button>
				</view>
			</view>
		</view>
		
		<view class="noMore" v-if="!list.length || !list">
			<image src="/static/none-course.png" mode="widthFix" class="no-data" />
			<view class="">暂无课程，去发布课程看看吧~</view>
		</view>
		
		<u-popup :show="show" :round="10" mode="bottom">
			<view class="item_box">
				<view class="item" v-for="(item,index) in typeList" :key="index" @click="checkType(index)">{{ item.name }}</view>
				<view class="item_red" @click="close('show')">取消</view>
			</view>
		</u-popup>
		
		<u-popup :show="showDel" :round="10" mode="center">
			<view class="pop-box">
				<view class="info">确认删除当前课程？</view>
				<view class="flex">
					<view class="close f1" @click="close('showDel')">取消</view>
					<view class="f1" @click="delCourse">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBar: 0,
				keyword: '',
				navdatas: [],
				status: 4,
				list: [],
				show: false,
				type: 1,
				typeIndex: 0,
				typeList: [
					{
						name: '按上架',
						type: 4,
					},
					{
						name: '按下架',
						type: 5,
					},
				],
				shop_id: '',
				class_id: '',
				last_page: 1,
				page: 1,
				showDel: false,
				id: '',
			}
		},
		onLoad(opt) {
			if(opt&& opt.type) {
				this.type = parseInt(opt.type);
			}
			this.getSystemInfo();
			this.shop_id = uni.getStorageSync('shopId');
		},
		onShow() {
			this.getCourseList();
			if(this.type == 2) {
				this.getClass();
			}
		},
		methods: {
			getSystemInfo() {
				uni.getSystemInfo({
					success: (e) => {
						this.statusBar = e.statusBarHeight
					}
				})
			},
			checkNav(i){
				this.class_id = i;
				this.getCourseList();
			},
			open(n) {
				this[n] = true;
			},
			close(n) {
				this[n] = false;
			},
			checkType(i) {
				this.typeIndex = i;
				this.status = this.typeList[i].type;
				this.show = false;
				this.getCourseList();
			},
			async getClass() {
				let res = await this.$api.get('staff/course/class',);
				if(res.code == 200) {
					this.navdatas = res.data;
					if(!this.class_id) {
						this.class_id = res.data[0].id;
					}
					this.getCourseList();
				}
			},
			async getCourseList() {
				let res = await this.$api.get('staff/course/list',{
					type: this.type,
					shop_id: this.shop_id,
					class: this.class_id,
					status: this.status,
					key: this.keyword,
					page: this.page,
				});
				if(res.code == 200) {
					this.list = res.data.data;
					this.last_page = parseInt(res.data.to)/parseInt(res.data.per_page);
				}
			},
			async onShelf(id) {
				let res = await this.$api.post(`staff/course/on/${id}`,{
					type: this.type,
					shop_id: this.shop_id
				});
				if(res.code == 200) {
					// console.log(res,1111);
					this.getCourseList();
					this.toast('操作成功')
				}
			},
			async offShelf(id){
				let res = await this.$api.post(`staff/course/off/${id}`,{
					type: this.type,
					shop_id: this.shop_id
				});
				if(res.code == 200) {
					// console.log(res,1111);
					this.getCourseList();
					this.toast('操作成功')
				}
			},
			delItem(id){
				this.id = id;
				this.showDel = true;
			},
			async delCourse() {
				let res = await this.$api.post(`staff/course/del/${this.id}`, {
					type: this.type,
					shop_id: this.shop_id
				});
				this.showDel = false;
				if(res.code == 200) {
					this.getCourseList();
					uni.showToast({
						title: res.msg
					});
				}
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.nav {
		font-size: 24rpx;
		background-color: #FFF;
		height: 44px;
		line-height: 44px;
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		z-index: 999;
		&_back_icon {
			width: 16rpx;
			height: 27rpx;
			margin-left: 27rpx;
			margin-right: 20rpx;
		}
		&_seach_box {
			background-color: #F5F5F5;
			border-radius: 30rpx;
			padding: 12rpx 36rpx;
			margin: 0 27rpx;
			margin-left: 20rpx;
		}
		.input-box {
			font-size: 27rpx;
			flex: 1;
			margin: 0 20rpx;
		}
		&_seach_icon {
			width: 25rpx;
			height: 25rpx;
		}
		&_sort-icon {
			width: 29rpx;
			height: 29rpx;
			margin-right: 10rpx;
		}
		&_filter {
			width: 29rpx;
			height: 29rpx;
			margin-right: 10rpx;
			margin-left: 80rpx;
		}
		&__class__type {
			justify-content: flex-end;
			margin-right: 20rpx;
		}
		&_right {
			margin-right: 27rpx;
		}
	}

	.tabs {
		background-color: #FFF;
		height: 44px;
		line-height: 44px;
		padding-left: 40rpx;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		z-index: 999;
		white-space: nowrap;
		&_item {
			display: inline-block;
			font-size: 27rpx;
			color: #B3B3B3;
			margin-right: 72rpx;
		}
		.item-name {
			line-height: 54rpx;
		}
		.act {
			color: #F17816;
		}
		&_line {
			height: 7rpx;
			background: #ffffff;
			width: 53rpx;
			bottom: 2px;
			border-radius: 100px;
			transition-property: transform;
			transition-duration: 300ms;
			margin: 0 auto;
		}
		&_line_act {
			background-color: #F17816;
		}
	}
	
	.course {
		font-size: 27rpx;
		padding: 0 20rpx;
		background-color: #fff;
		margin: 20rpx 27rpx;
		border-radius: 20rpx;
		&_content {
			padding: 20rpx 0;
			font-size: 29rpx;
			&_img {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
			}
			&_label {
				font-size: 27rpx;
				color: #B3B3B3;
				margin-top: 10rpx;
			}
			&_price {
				color: #F51818;
				text-align: right;
				.symbol {
					font-size: 27rpx;
				}
				.fw-b {
					font-family: PingFang SC Bold;
					font-size: 32rpx;
				}
				.util {
					font-size: 24rpx;
				}
			}
		}
		&_bottom {
			padding-top: 10rpx;
			padding-bottom: 30rpx;
			justify-content: flex-end;
			font-size: 24rpx;
			.btn {
				margin: 0;
				padding: 0;
				padding: 0 24rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 500;
				height: 47rpx;
				line-height: 47rpx;
				min-width: 93rpx;
				text-align: center;
				margin-left: 24rpx;
				background-color: #FFF;
				min-width: 120rpx;
			}
			.org_btn {
				background-color: #F17816;
				color: #fff;
			}
			.del-icon-gray {
				width: 23rpx;
				height: 23rpx;
				margin-right: 8rpx;
			}
			&_box {
				justify-content: flex-end;
			}
		}
	}
	.noMore {
		font-size: 27rpx;
		line-height: 1;
		padding: 30% 0;
	}
	.no-data {
		width: 450rpx;
		height: 450rpx;
	}
	.item_box {
		background-color: #F5F5F5;
		border-radius: 20rpx 20rpx 0 0;
		overflow: hidden;
		.item,.item_red {
			height: 98rpx;
			line-height: 98rpx;
			text-align: center;
			font-size: 32rpx;
			background-color: #FFF;
			border-bottom: 2rpx solid #F5F5F5;
		}
		.item_red {
			color: #F11A1A;
			margin-top: 20rpx;
		}
		.item:last-child {
			border-bottom: none;
		}
	}
</style>